#!/bin/sh
echo -n 'Page Name:'
read page

author='吴建涛(fortomorrow@163.com)'
date=`date`

#js模块
mkdir -p app/$page/mods
#less模块
mkdir -p app/$page/style

echo "/**
 * Page: $page
 * Author: $author
 * Date: $date
 */
require([], function(){
});
" >> app/$page/index.js
echo "app/$page/index.js 创建成功"

echo "/**
 * Page: $page
 * Author: $author
 * Date: $date
 */
@import (inline) '../com/third/bootstrap.min.css';
@import (inline) '../com/third/normalize.min.css';
" >> app/$page/index.less
touch app/$page/index.css
echo "app/$page/index.less 创建成功"

echo "<!DOCTYPE html>
<html lang=\"zh\">
<head>
  <meta charset=\"UTF-8\">
  <title>$page</title>

  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">

  <!--移动端常用
    <link rel=\"apple-touch-icon\" href=\"apple-touch-icon.png\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />
  -->

  <link rel=\"stylesheet\" href=\"$page/index.css\" type=\"text/css\" charset=\"utf-8\">
</head>
<body>
  <script>
  (function(){
    var requirepath;
    var main;
    var config;

    if(window.location.search.indexOf('debug') !== -1){
      requirepath = 'com/third/require.min.js';
      main = 'test/index.js';
      config = 'test/config.js';
    }else{
      requirepath = './require.min.js';
      main = 'test/index-min.js';
    }

    var requireScript = document.createElement('script');
    requireScript.src = requirepath;
    document.body.appendChild(requireScript)

    if(config){
      var configScript = document.createElement('script');
      configScript.src = config;
      document.body.appendChild(configScript)
    }

    var mainScript = document.createElement('script');
    mainScript.src = main;
    document.body.appendChild(mainScript)
  })();
  </script>
</body>
</html>
" >> app/${page}.html
echo "app/${page}.html 创建成功"

cat app/com/config.js | sed -e 's/<%page%>/$page/g' > app/$page/config.js
echo "app/$page/config.js 创建成功"

echo "
mkdir -p build/$page
cp app/${page}.html build/
lessc app/$page/index.less > build/$page/index.css
r.js -o mainConfigFile=app/$page/config.js name=index include=index,config out=build/$page/index-min.js baseUrl=app/$page" >> build.sh
